import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.parent.excel.ExcelFieldMap;
import demo.pojo.classdemo.AClass;
import demo.pojo.classdemo.BClass;
import demo.pojo.classdemo.CClass;
import demo.pojo.excl.ExcelDataOther;
import com.parent.utils.ClassToUtils;
import com.parent.utils.ExcelUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author ${USER}
 * @Date ${DATE} ${TIME}
 * @Description
 */
public class Main {
    public static void main(String[] args) throws Exception {


        String imageUrl = "https://naoyihui-patient.oss-cn-shanghai.aliyuncs.com/a1f2ead20f7b455b8b4c2b205a5ce9c7.png";
        boolean b = checkImgExist(imageUrl);
        System.out.println(b);
        try {
            URL url = new URL(imageUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("HEAD");
            connection.connect();

            int responseCode = connection.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                InputStream inputStream = url.openStream();
                BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
                BufferedImage image = ImageIO.read(bufferedInputStream);

                if (image!= null) {
                    System.out.println("The given URL is an image.");
                } else {
                    System.out.println("The given URL is not an image.");
                }
            } else {
                System.out.println("The given URL is not an image.");
            }
        } catch (IOException e) {
            System.err.println("Error while checking if the given URL is an image: " + e.getMessage());
        }

        String s1 = "[{\"correlationId\":95,\"count\":0,\"id\":70,\"name\":\"李金钋第三个标签\",\"teamId\":7,\"teamName\":\"李金钋的团队\",\"weight\":0},{\"correlationId\":66,\"count\":0,\"id\":41,\"name\":\"李金钋第一个标签\",\"teamId\":7,\"teamName\":\"李金钋的团队\",\"weight\":1},{\"correlationId\":94,\"count\":0,\"id\":69,\"name\":\"李金钋第二个标签\",\"teamId\":7,\"teamName\":\"李金钋的团队\",\"weight\":2}]";
        //Json转List
        JSONArray objects =JSONUtil.parseArray(s1);
        System.out.println(objects);
        String s = "{\"type\":\"image\",\"url\":\"//naoyihui-patient.oss-cn-shanghai.aliyuncs.com/d1f94f7424454b24a7f99178cac52c13.jpg\"}";

        JSONObject jsonObject = JSONUtil.parseObj(s);
        System.out.println(jsonObject);
//        excelUtilsDemo();
//        ClassToUtilsDemo1();
//        ClassToUtilsDemo2();
//        ClassToUtilsDemo3();
//        ClassToUtilsDemo4();

    }

    public static boolean checkImgExist(String urlString){
        URL url;
        URLConnection urlConnection;
        try {
            url = new URL(urlString);
            urlConnection =  url.openConnection();
            int status =  ((HttpURLConnection) urlConnection).getResponseCode();
            if(status==200){
                return true;
            }
        } catch (Exception e1) {
            System.out.println("图片不存在!");
        }
        return false;
    }

    public static void excelUtilsDemo() {
        System.out.println("excelUtilsDemo");
        List<ExcelFieldMap> allFieldMap = ExcelUtils.getAllFieldMaps(ExcelDataOther.class);
        System.out.println(allFieldMap);
        List<ExcelFieldMap> allMustFieldMaps = ExcelUtils.getAllMustFieldMaps(ExcelDataOther.class);
        System.out.println(allMustFieldMaps);
        System.out.println("end");
    }

    public static void ClassToUtilsDemo1() throws Exception {
        System.out.println("ClassToUtilsDemo1");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id",1);
        map.put("name","号");
        map.put("age",1);
        AClass aClass = ClassToUtils.mapToObject(map, AClass.class);
        System.out.println(aClass);
        System.out.println("end");
    }
    public static void ClassToUtilsDemo2() throws Exception {
        System.out.println("ClassToUtilsDemo2");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id",1);
        map.put("name","号");
        map.put("age",1);
        CClass aClass = ClassToUtils.mapToObject(map,CClass.class);
        System.out.println(aClass);
        System.out.println("end");
    }
    public static void ClassToUtilsDemo3() throws Exception {
        System.out.println("ClassToUtilsDemo3");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id",1);
        map.put("name","号");
        map.put("age",2);
        AClass aClass = ClassToUtils.mapToObject(map, AClass.class);
        System.out.println(aClass);

        BClass bClass = ClassToUtils.objectToOtherObject(aClass, BClass.class);
        System.out.println(bClass);

        Map<String, String> hashMap = new HashMap<>();
        hashMap.put("age","id");

        CClass cClass = ClassToUtils.objectToOtherObjectKeyNameReplace(aClass, CClass.class, hashMap);
        System.out.println(cClass);
        System.out.println("end");
    }

    public static void ClassToUtilsDemo4() throws Exception {
        System.out.println("ClassToUtilsDemo4");
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("id",1);
        map.put("name","sad");
        map.put("age",2);
        AClass aClass = ClassToUtils.mapToObject(map, AClass.class);
        System.out.println(aClass);
        System.out.println("===");

        ClassToUtils.objectStringSetNotNull(aClass);
        System.out.println(aClass);
        System.out.println("end");
    }
}